package yunquan.wang.modules.customer.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import yunquan.wang.base.dao.PropertyFilter;
import yunquan.wang.base.web.WebConstants;
import yunquan.wang.base.web.WebHelper;
import yunquan.wang.base.web.ajax.AjaxDone;
import yunquan.wang.modules.customer.entity.BusinessRecord;
import yunquan.wang.modules.customer.entity.ContactsVisitInfo;
import yunquan.wang.modules.customer.service.BusinessRecordService;
@Controller
@RequestMapping("/customer/busirecord")
public class BusinessRecordController {
	@Resource
	private BusinessRecordService service;
	
	@RequestMapping(value = "/list.do")
	public String list(HttpServletRequest request, Model model) {
		Pageable page = WebHelper.getPage(request);
		List<PropertyFilter> filters = WebHelper.filterRequest(request);
		model.addAttribute(WebConstants.ATTRIBUTE_PAGE,service.findPage(page, filters));
		return "customer/busirecordList";
	}
	
	@RequestMapping(value = "/save.do")
	public @ResponseBody AjaxDone save(@Valid BusinessRecord entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return WebHelper.validFail(bindingResult);
		} else {
			try {
				entity = service.save(entity);
			} catch (Exception e) {
			return	WebHelper.saveFail( e);
			}
		}
		return WebHelper.saveSuccess();
	}
	 
	@RequestMapping(value = "/view.do")
	public String view(String id,  Model model) {
		model.addAttribute(WebConstants.ATTRIBUTE_ENTITY, service.get(id));
		return "customer/busirecordView";
	}
	 
	@RequestMapping(value = "/input.do")
	public String input(String id,Model model) {
		BusinessRecord entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = service.get(id);
		} else {
			entity = new BusinessRecord();
		}
		model.addAttribute(WebConstants.ATTRIBUTE_ENTITY, entity);
		return "customer/busirecordInput";
	}
	
	@RequestMapping(value = "/delete.do")
	public @ResponseBody AjaxDone delete(String [] id) {
		for (String ids : id) {
			service.delete(ids);
		}
		return WebHelper.deleteSuccess();
	}
 

}
